package main.test.SparkAPI.Baics.collectapi

object testList {
  def callPrint(x:List[Any]): Unit ={
    println(x.mkString("LIST( ",", "," )"))
  }
  def printL(x:List[Any]): Unit ={
    callPrint(x)
  }
  def testLst(): Unit ={
    // how to create an initial List
    val ints = List(1,2,3)
    printL(ints)
    val names = List("Joel", "Chris", "Ed")
    printL(names)

    // Adding elements to a List
    /**
     *  Instead you create a new list by prepending or appending elements to an existing List. For instance, given this List:
     */
    val a = List(1,2,3)
    val b = 0 +: a
    val c = List(-1, 0) ++: a
    printL(a)
    printL(b)
    printL(c)

    // How to remember the method names
    /**
     *  one way to remember those method names
     *  is to think that the : character
     *  represents the side that the sequence is on, so when you use +:
     *  you know that the list needs to be on the right, like this
     */
    printL( 0 +: a)

    /**
     * TODO: One good thing about these method names:
     *      they’re consistent. The same method names
     *      are used with other
     *      immutable sequence classes, such as Seq and Vector.
     */

    // lOOP over lists
    // you can print each string like this:
    for (name <- names) println(name)

    /**
     * TODO: A great thing about this
     *      approach is that it works
     *      with all sequence classes,
     *      including ArrayBuffer, List, Seq, Vector, etc.
     */

      // you can also create the exact same list this way:
    val spList = 1 :: 2 :: 3 :: Nil
    printL(spList)

  }
  def main(args: Array[String]): Unit = {
    // The List class is a linear,
    // immutable sequence.
    // All this means is
    // that it’s a linked-list that
    // you can’t modify. Any time you want
    // to add or remove List elements,
    // you create a new List from an existing List.
    testLst()
  }

}
